package com.cskaoyan.demo2.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/8 19:29
 */


public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T read(String jsonStr, Class<T> clazz) {
        T t = null;

        try {
            t = objectMapper.readValue(jsonStr,clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    public static String write(Object instance) {
        String jsonStr = null;
        try {
            jsonStr = objectMapper.writeValueAsString(instance);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return jsonStr;
    }


}
